package com.itheima.thread.utils.exchanger.demo01;

import java.util.concurrent.Exchanger;

public class ExchangerDemo {

    public static void main(String[] args) {

        Exchanger<String> exchanger = new Exchanger<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String s1 = "线程1生成的数据";
                try {
                    String exchange = exchanger.exchange(s1);
                    System.out.println("线程1接收到了" + exchange);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String s2 = "线程2生成的数据";
                try {
                    String exchange = exchanger.exchange(s2);
                    System.out.println("线程2接收到了" + exchange);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}
